<template>
  <div class="sign-in-view">
    <div class="content">
      <div class="calendar-container">
        <el-calendar v-model="currentDate" />
      </div>
      <div class="sign-in-button-container">
        <button v-if="!signedInToday" @click="signIn">签到</button>
        <div v-else class="signed-in-text">今天已签到</div>
      </div>
    </div>
  </div>
  <div class="bottom-fixed-button">
    <el-button type="primary" style="width: 90%;" @click="returnToTechnician">返回菜单</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
import dayjs from 'dayjs';

const router = useRouter();
const signedInToday = ref(false);
const currentDate = ref(dayjs().format('YYYY-MM-DD'));

const checkSignInStatus = async () => {
  try {
    const response = await axios.get('http://localhost:8080/sign-in1/check');
    signedInToday.value = response.data;
  } catch (error) {
    console.error('Error checking sign-in status:', error);
  }
};

const signIn = async () => {
  try {
    const response = await axios.post('http://localhost:8080/sign-in1/signin');
    if (response.data) {
      signedInToday.value = true;
    }
  } catch (error) {
    console.error('Error signing in:', error);
  }
};

onMounted(() => {
  checkSignInStatus();
});

const returnToTechnician = () => {
  router.push('/technician/home');
};
</script>

<style scoped>
.sign-in-view {
  display: flex;
  flex-direction: column;
  background-color: white;
}

.signed-in-text {
  width: 100%;
  padding: 10px 0;
  font-size: 16px;
  text-align: center;
  border: 1px solid #dcdcdc; /* Add border for signed-in text */
  border-radius: 4px;
}


.content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.calendar-container {
  width: 100%;
  max-width: 400px;
}

.sign-in-button-container {
  width: 100%;
  max-width: 400px;
  display: flex;
  justify-content: center;
}

button {
  width: 100%;
  padding: 10px 0;
  font-size: 16px;
}

.bottom-fixed-button {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
